<template>
  <div class="app-container">
    <div style="margin-bottom: 4px;">
      <el-button type="primary" size="mini" @click="handleAdd()">新增</el-button>
      <el-button type="danger" size="mini" @click="handleDeleteAll()">删除</el-button>
    </div>
    <el-table ref="multipleTable" :data="list" v-loading="listLoading" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column label="账号">
        <template slot-scope="scope">
          {{scope.row.username}}
        </template>
      </el-table-column>
      <el-table-column label="角色">
        <template slot-scope="scope">
          <span>{{scope.row.roleName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="绑定手机">
        <template slot-scope="scope">
          <span>{{scope.row.phone}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
         <!-- <el-button
            size="mini"
            @click="handleChangePW(scope.$index, scope.row)">修改密码</el-button>-->
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 4px;">
      <el-pagination
        background
        layout="total, prev, pager, next"
        :page-size="pageSize"
        :current-page="pageNum"
        :total="pageTotal"
        @current-change="pageChange">
      </el-pagination>
    </div>

    <el-dialog :title="dialogFormTitle" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="账号" :label-width="formLabelWidth">
          <el-input v-model="form.username" :disabled="codeDisabled"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="绑定手机" :label-width="formLabelWidth">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="角色" :label-width="formLabelWidth">
          <el-select v-model="form.roleIdSelect" multiple placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.roleName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getList, add, update, deleted, getRole } from '@/api/user'

  export default {
    data() {
      return {
        pageNum: 1, // 当前页
        pageSize: 20, // 每页数
        pageTotal: 0, // 总页数
        list: null,
        listLoading: true,
        dialogFormVisible: false,
        dialogFormTitle: '',
        formLabelWidth: '100px',
        form: {},
        codeDisabled: true,
        options: [] // 角色选择框数据
      }
    },
    filters: {
    },
    created() {
      this.fetchData()
      this.initRoleData()
    },
    methods: {
      fetchData() {
        this.listLoading = true
        getList(this.pageNum, this.pageSize).then(response => {
          console.log(response)
          for (let i = 0; i < response.list.length; i++) {
            if(response.list[i].roleId != null){
              const roleIds = response.list[i].roleId.split(',')
            const roleIdsInt = []
            for (let j = 0; j < roleIds.length; j++) {
              roleIdsInt.push(Number(roleIds[j]))
            }
            response.list[i].roleIdSelect = roleIdsInt
            }
            
          }
          this.list = response.list
          this.listLoading = false
          this.pageTotal = response.total
          this.pageNum = response.pageNum
        })
      },
      initRoleData() {
        getRole().then(response => {
          console.log(response)
          this.options = response.data
        })
      },
      handleAdd() {
        this.form = {
          roleIdSelect: []
        }
        this.codeDisabled = false
        this.dialogFormTitle = '新增'
        this.dialogFormVisible = true
      },
      handleDeleteAll() {
        console.log(this.$refs.multipleTable.selection)
        const selectedData = this.$refs.multipleTable.selection
        if (selectedData.length <= 0) {
          this.$message({
            type: 'info',
            message: '请选择至少一行数据'
          })
          return
        }
        let ids = ''
        for (let i = 0; i < selectedData.length; i++) {
          ids += selectedData[i].id + ','
        }
        if (ids.length > 0) {
          ids = ids.substring(0, ids.length - 1)
        }
        this.$confirm('确认删除选中的' + selectedData.length + '条数据', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleted(ids).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.fetchData()
          })
        })
      },
      handleEdit(index, row) {
        this.form = row
        this.codeDisabled = true
        this.dialogFormTitle = '编辑'
        this.dialogFormVisible = true
      },
      handleChangePW() {
        // 修改密码
      },
      handleDelete(index, row) {
        this.$confirm('确认删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleted(row.id + '').then(response => {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.fetchData()
          })
        }).catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消删除'
          // })
        })
      },
      submit() {
        console.log(this.form)
        let roleId = ''
        for (let i = 0; i < this.form.roleIdSelect.length; i++) {
          roleId += this.form.roleIdSelect[i] + ','
        }
        if (roleId.length > 0) {
          roleId.substring(0, roleId.length - 1)
        }
        this.form.roleId = roleId
        if (this.form.id && this.form.id !== '') {
          // 编辑
          update(this.form).then(response => {
            this.dialogFormVisible = false
            this.fetchData()
          })
        } else {
          // 新增
          add(this.form).then(response => {
            this.dialogFormVisible = false
            this.fetchData()
          })
        }
      },
      pageChange(val) {
        this.pageNum = val
        this.fetchData()
      }
    }
  }
</script>
